package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.AgentSettleMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentSettle;
import com.weixing.mall.provider.model.dto.AgentSettleEditDto;
import com.weixing.mall.provider.model.enums.InfoPerfectEnum;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.service.IAgentSettleService;
import com.weixing.mall.provider.util.InfoPerfectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 代理商模块-代理商结算卡信息表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentSettleServiceImpl extends ServiceImpl<AgentSettleMapper, AgentSettle> implements IAgentSettleService {
    @Autowired
    private IAgentService agentService;

    @Override
    public Long saveSettle(AgentSettleEditDto dto) {
        AgentSettle settle = new AgentSettle();
        settle.setId(dto.getId());
        settle.setAgentId(dto.getAgentId());
        settle.setBankBranch(dto.getBankBranch());
        settle.setBankCode(dto.getBankCode());
        settle.setBankCard(dto.getBankCard());
        settle.setBankCardBack(dto.getBankCardBack());
        settle.setBankCardFront(dto.getBankCardFront());

        Agent agent = agentService.getById(dto.getAgentId());
        int flag = agent.getIsInfoPerfect();
        agent.setIsInfoPerfect(InfoPerfectUtil.add(flag, InfoPerfectEnum.SETTLE));
        agentService.updateById(agent);
        if (null == dto.getId()) {
            if(dto.getAgentId()!=null){
                save(settle);
            }
        } else {
            updateById(settle);
        }
        return settle.getId();
    }

    @Override
    public AgentSettle selectByAgentId(Long agentId) {
        return getOne(new QueryWrapper<AgentSettle>().eq("agent_id",agentId));
    }
}
